package service

import (
	"errors"
	"gorm.io/gorm"
	"shop-bean/dto"
	"shop-bean/model"
	"shop-bean/request"
	"shop-common/library/loggerV2"
	"shop-common/library/variables"
	"shop-common/utils/queryWrapper"
	"shop-common/utils/uploader"
	"shop-service/repository"
	"strings"
)

type IStoreCategoryService interface {
	FindJoin(_type uint8) ([]*dto.StoreCategoryDTO, error)
	FindSimple(_type uint8) ([]*model.StoreCategory, error)
	FindById(categoryId uint, _type uint8) (info *model.StoreCategory, err error)
	FindOne(pair *queryWrapper.WherePair) (*model.StoreCategory, error)
	Updates(request *request.StoreCategoryRequest) error
}

type StoreCategoryService struct {
	db         *gorm.DB
	repository repository.IStoreCategoryRepository
}

func NewStoreCategoryService() IStoreCategoryService {
	return &StoreCategoryService{variables.GormDB(), repository.NewStoreCategoryRepository()}
}

func (s *StoreCategoryService) FindJoin(_type uint8) (list []*dto.StoreCategoryDTO, err error) {
	rows, err := s.repository.FindJoin(s.db, _type, nil)
	if err != nil {
		return nil, err
	}
	for _, entity := range rows {
		d := &dto.StoreCategoryDTO{
			CategoryId:   entity.CategoryId,
			CategoryName: entity.CategoryName,
			ParentId:     entity.ParentId,
			Seq:          entity.Seq,
			Status:       entity.Status,
			Type:         entity.Type,
		}

		if entity.IconId > 0 {
			d.IconAttachment = &dto.AttachmentDTO{
				AttId: entity.AttId,
				Url:   strings.Join([]string{uploader.GetHostStatic(entity.ImageType), entity.AttDir, entity.Name}, ""),
			}
		}

		list = append(list, d)
	}
	return list, nil
}

func (s *StoreCategoryService) FindSimple(_type uint8) ([]*model.StoreCategory, error) {
	return s.repository.FindSimple(s.db, _type)
}

func (s *StoreCategoryService) FindById(categoryId uint, _type uint8) (info *model.StoreCategory, err error) {
	//rows, err := s.repository.FindJoin(s.db, _type, &categoryId)
	//if err != nil {
	//	return nil, err
	//}
	//
	//for _, entity := range rows {
	//	if info == nil {
	//		info = &dto.StoreCategoryDTO{
	//			CategoryId:   entity.CategoryId,
	//			CategoryName: entity.CategoryName,
	//			ParentId:     entity.ParentId,
	//			Seq:          entity.Seq,
	//			Status:       entity.Status,
	//			Type:         entity.Type,
	//		}
	//	}
	//
	//	if info.BigAttachment == nil && entity.BigId > 0 {
	//		info.BigAttachment = &dto.AttachmentDTO{
	//			AttId: entity.AttId,
	//			Url:   strings.Join([]string{util.GetHostStatic(entity.ImageType), entity.AttDir, entity.Name}, ""),
	//		}
	//	} else if info.IconAttachment == nil && entity.IconId > 0 {
	//		info.IconAttachment = &dto.AttachmentDTO{
	//			AttId: entity.AttId,
	//			Url:   strings.Join([]string{util.GetHostStatic(entity.ImageType), entity.AttDir, entity.Name}, ""),
	//		}
	//	}
	//}

	return s.repository.FindById(s.db, categoryId, _type)
}

func (s *StoreCategoryService) FindOne(pair *queryWrapper.WherePair) (category *model.StoreCategory, err error) {
	category, err = s.repository.FindOne(s.db, pair)
	if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
		loggerV2.Errorf("find by pair storeCategory failed: err %v, pair %+v", err, pair)
		return nil, err
	}
	return category, nil
}

func (s *StoreCategoryService) Updates(request *request.StoreCategoryRequest) error {
	columns := map[string]interface{}{
		"parent_id":     request.ParentId,
		"category_name": request.CategoryName,
		"status":        request.Status,
		"seq":           request.Status,
	}

	if request.BigAttachment != nil {
		columns["big_id"] = request.BigAttachment.AttId
	}

	if request.IconAttachment != nil {
		columns["icon_id"] = request.IconAttachment.AttId
	}

	err := s.repository.Updates(s.db, request.CategoryId, columns)
	if err != nil {
		loggerV2.Errorf("by category updates storeCategory failed: %v, categoryId: %d", columns, request.CategoryId)
		return err
	}

	return nil
}
